//
//  MusicManager.m
//  NvSellerShow
//
//  Created by dx on 17/2/9.
//  Copyright © 2017年 Meicam. All rights reserved.
//

#import "MusicManager.h"
#import "MusicInfo.h"

@interface MusicManager()
@property (nonatomic, strong) NSArray *musicArray;
@property (nonatomic, strong) NSMutableDictionary *musicDic;
@end

@implementation MusicManager

+ (NSString *)phonetic:(NSString*)sourceString
{
    NSMutableString *source = [sourceString mutableCopy];
    CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformMandarinLatin, NO);
    CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformStripDiacritics, NO);
    return source;
}

- (instancetype)initWithArray:(NSArray *)array
{
    if (self = [super init]) {
        _musicArray = array;
    }
    return self;
}

- (NSDictionary *)divideIntoGroup
{
    for(MusicInfo* music in _musicArray)
    {
        NSString *nameInEnglish = [MusicManager phonetic:music.musicTitle];
        //如果为空跳出
        if ([nameInEnglish isEqualToString:@""]) {
            continue;
        }
        nameInEnglish = [nameInEnglish capitalizedString];
        unichar k = [nameInEnglish characterAtIndex:0];
        if (!(k >= 'A' && k <= 'Z')) {
            k = '#';
        }
        NSString *key = [NSString stringWithFormat:@"%c",k];
        
        NSMutableArray *arrayGroupK = [self.musicDic objectForKey:key];
        if (!arrayGroupK) {
            arrayGroupK = [[NSMutableArray alloc]initWithCapacity:5];
            [arrayGroupK addObject:music];
            if (nil == self.musicDic) {
                self.musicDic = [[NSMutableDictionary alloc]initWithCapacity:5];
            }
            [self.musicDic setObject:arrayGroupK forKey:key];
        }else{
            [arrayGroupK addObject:music];
        }
    }
    
    return self.musicDic;
}

@end
